package com.turman.glsample.base

import javax.microedition.khronos.egl.EGLConfig
import android.opengl.GLES20
import android.opengl.GLES20.GL_FLOAT
import android.opengl.GLES20.GL_LINES
import android.opengl.GLES20.GL_POINTS
import android.opengl.GLES20.GL_TRIANGLES
import android.opengl.GLES20.glDisableVertexAttribArray
import android.opengl.GLES20.glDrawArrays
import android.opengl.GLES20.glUniform4f
import android.opengl.GLSurfaceView
import com.turman.glsample.MyApplication
import com.turman.glsample.R
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer
import javax.microedition.khronos.opengles.GL10

class SampleRenderer : GLSurfaceView.Renderer {
    companion object {
        private const val POSITION_COMPONENT_COUNT = 2
        private const val BYTES_PER_FLOAT = 4
    }

    private val tableVerticesWithTriangles: FloatArray = floatArrayOf(
        -0.5F, -0.5F,
        0.5F, 0.5F,
        -0.5F, 0.5F,

        -0.5F, -0.5F,
        0.5F, -0.5F,
        0.5F, 0.5F,
        // 中线
        -0.5F, 0F,
        0.5F, 0F,
        // 顶点
        0F, -0.25F,
        0F, 0.25F,
    )

    //创建一个native内存用于存储顶点数据
    private val vertexData: FloatBuffer = ByteBuffer
        .allocateDirect(tableVerticesWithTriangles.size * BYTES_PER_FLOAT)
        .order(ByteOrder.nativeOrder())
        .asFloatBuffer()
        .put(tableVerticesWithTriangles)

    private var programId: Int = -1

    override fun onSurfaceCreated(p0: GL10?, p1: EGLConfig?) {
        GLES20.glClearColor(1F, 0F, 0F, 1F) //清理颜色 红

        //读取shader代码
        val vertexShaderCode =
            MyApplication.application?.readStringFromRaw(R.raw.simple_vertex_shader)
        val fragmentShaderCode =
            MyApplication.application?.readStringFromRaw(R.raw.simple_fragment_shader)

        //创建程序
        if (GLES20.glIsProgram(programId)) {
            GLES20.glDeleteProgram(programId)
            programId = -1
        }
        programId = GLES20.glCreateProgram()

        //给程序设置shader
        GLES20.glAttachShader(programId, loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode!!))
        GLES20.glAttachShader(
            programId,
            loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode!!)
        )
        //链接程序
        GLES20.glLinkProgram(programId)
        //验证程序
        GLES20.glValidateProgram(programId)

        vertexData.position(0)
    }

    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
        GLES20.glViewport(0, 0, width, height)
    }

    override fun onDrawFrame(gl: GL10?) {
        //GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)

        GLES20.glUseProgram(programId)
        val uColorLocation = GLES20.glGetUniformLocation(programId, "u_Color")
//        GLES20.glEnableVertexAttribArray(uColorLocation)
        val aPositionLocation = GLES20.glGetAttribLocation(programId, "a_Position")
        GLES20.glEnableVertexAttribArray(aPositionLocation)
        GLES20.glVertexAttribPointer(
            aPositionLocation,
            POSITION_COMPONENT_COUNT,
            GL_FLOAT,
            false,
            POSITION_COMPONENT_COUNT * BYTES_PER_FLOAT,
            vertexData
        )

        glUniform4f(uColorLocation, 0.2f, 0.2f, 0.2f, 1f)
        glDrawArrays(GL_TRIANGLES, 0, 6) //绘制平面
        glUniform4f(uColorLocation, 1F, 0F, 0F, 1F)
        glDrawArrays(GL_LINES, 6, 2) //绘制线
        glUniform4f(uColorLocation, 0F, 0F, 1F, 1F)
        glDrawArrays(GL_POINTS, 8, 1) //绘制点
        glUniform4f(uColorLocation, 1F, 0F, 0F, 1F)
        glDrawArrays(GL_POINTS, 9, 1) //绘制点

        glDisableVertexAttribArray(aPositionLocation)
//        glDisableVertexAttribArray(uColorLocation)
    }
}
